package app.netant.galgame.bangumi.cartoon;

import java.util.HashSet;
import java.util.Set;

import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.db.sql.DBManager;
import cn.lily.com.db.sql.manager.DBOfflineManager;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.lang.number.MathUtil;

public class BangumiAntRoot {
	
	private static String pageUrl = "http://bangumi.tv/anime/browser?page=";
	
	public static void extTags(int maxIdx){
		
		Set<String> result = new HashSet<String>();
		
		System.out.println("整体处理开始........");
		
		for(int i=1;i<=maxIdx;i++){
			
			String url = pageUrl + i;
			
			System.out.println("第"+i+"页数据开始处理！");
			
			try {
				BangumiAntPage page = new BangumiAntPage(url);
				Set<String> keys = page.extTags();
				for(String key:keys){
					result.add(key);
				}
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
			
		}
		
		System.out.println("输出所有标签");
		
		for(String rs:result){
			System.out.println(rs);
		}
		
		System.out.println("整体处理结束........");
		
	}
	
	public static void analyse(int maxIdx){
		
		resume();
		
		System.out.println("整体处理开始........");
		
		for(int i=1;i<=maxIdx;i++){
			
			String url = pageUrl + i;
			
			System.out.println("第"+i+"页数据开始处理！");
			
			try {
				BangumiAntPage page = new BangumiAntPage(url);
				
				page.analyse();
				
			} catch (Exception e) {
				e.printStackTrace();
				getDBManager().executeUpdate("insert into ${schema}.LEO_BANGUMI_CARTOON_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
						+ "values ('"+System.currentTimeMillis()+""+MathUtil.random(3)+"','"+url+"','1') ");	
				continue;
			}
			
		}
		
		System.out.println("整体处理结束........");
		
	}
	
	public static void analyse(int minIdx,int maxIdx){
		
		System.out.println("整体处理开始........");
		
		for(int i=minIdx;i<=maxIdx;i++){
			
			String url = pageUrl + i;
			
			System.out.println("第"+i+"页数据开始处理！");
			
			try {
				BangumiAntPage page = new BangumiAntPage(url);
				
				page.analyse();
				
			} catch (Exception e) {
				e.printStackTrace();
				getDBManager().executeUpdate("insert into ${schema}.LEO_BANGUMI_CARTOON_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
						+ "values ('"+System.currentTimeMillis()+""+MathUtil.random(3)+"','"+url+"','1') ");	
				continue;
			}
			
		}
		
		System.out.println("整体处理结束........");
		
	}
	
	public static void excep(){
		
		System.out.println("异常处理开始........");
		
		getDBManager().executeUpdate("delete from ${schema}.LEO_BANGUMI_CARTOON_INFO "
				+ "where CAR_URL in "
				+ "(select EXCEP_URL from ${schema}.LEO_BANGUMI_CARTOON_EXCEP)");
		
		DataTable exptable = getDBManager().executeQueryTable("select * from ${schema}.LEO_BANGUMI_CARTOON_EXCEP");
		
		while(exptable.next()){
			
			try {
				
				String level = exptable.getString("EXCEP_LEVEL");
				String href = exptable.getString("EXCEP_URL");
				
				if("1".equals(level)){
					
					BangumiAntPage page = new BangumiAntPage(href);
					
					page.analyse();
					
				}else if("2".equals(level)){
					
					DataTable dataTable = getDBManager().executeQueryTable("select * from ${schema}.LEO_BANGUMI_CARTOON_INFO where CAR_URL='"+href+"'");
					if(dataTable.next()){
						continue;
					}
					
					System.out.println("开始连接网站：" + href);

					BangumiAntDetail detail = new BangumiAntDetail(href);

					System.out.println("网站连接成功！");
					
					BangumiCartoonEntity entity = new BangumiCartoonEntity();
					entity.setCarUrl(href);
					detail.setEntity(entity);
					
					detail.analyse();
					
				}
				
				getDBManager().executeUpdate("delete from ${schema}.LEO_BANGUMI_CARTOON_EXCEP where EXCEP_ID='"+exptable.getString("EXCEP_ID")+"'");
				
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
			
		}
		
		System.out.println("异常处理结束........");

	}
	
	public static DBManager getDBManager(){
		return DBOfflineManager.getDBManager();
	}
	
	/**
	 * 断点续传
	 */
	public static void resume(){
		
		System.out.println("断点续传处理开始........");
		
		getDBManager().executeUpdate("delete from ${schema}.LEO_BANGUMI_CARTOON_INFO "
				+ "where CAR_URL in "
				+ "(select EXCEP_URL from ${schema}.LEO_BANGUMI_CARTOON_EXCEP)");
		
		getDBManager().executeUpdate("delete from ${schema}.LEO_BANGUMI_CARTOON_EXCEP");
		
		System.out.println("断点续传处理结束........");
		
	}
	
	public static void main(String[] args) {
		Enviroment.createEnv();
		Enviroment.getEnv().getLogBean().beginLog();
		//BangumiAntRoot.extTags(10);
		BangumiAntRoot.analyse(592);
		//BangumiAntRoot.excep();
		//BangumiAntRoot.analyse(494, 576);
	}
}
